package com.food.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.food.dao.service.CourseService;
import com.food.dao.service.ManagerService;
import com.food.model.Course;
import com.food.model.Manager;

/**
 * Servlet implementation class CourseServlet
 */
@WebServlet("/manager/CourseServlet")
public class CourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	private static SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		String type = request.getParameter("type");
		if(type!=null && type.equals("DELETE")){
			int cid = Integer.parseInt(request.getParameter("cid"));
			Course c = new Course();
			c.setCid(cid);
			CourseService courseService = new CourseService();
			courseService.deleteCourse(c);
			RequestDispatcher rd = request
					.getRequestDispatcher("/course/DisplayCourse?type=MANAGER");
			rd.forward(request, response);
			return;
			
		}
		
		String cname = request.getParameter("cname");
		String cteacher = request.getParameter("cteacher");
		String cstartdateStr = request.getParameter("cstartdate");
		String cenddateStr = request.getParameter("cenddate");
		String cpeopleStr = request.getParameter("cpeople");
		String cpriceStr = request.getParameter("cprice");
		String cplace = request.getParameter("cplace");
		String cintroduce = request.getParameter("cintroduce");
		
		java.sql.Date cstartdate=null;
		java.sql.Date cenddate=null;

		java.util.Date temp1;
		try {
			temp1 = sFormat.parse(cstartdateStr);
			cstartdate = new java.sql.Date(temp1.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		java.util.Date temp2;
		try {
			temp2 = sFormat.parse(cenddateStr);
			cenddate = new java.sql.Date(temp2.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		
		int cpeople = Integer.parseInt(cpeopleStr);
		int cprice = Integer.parseInt(cpriceStr);
		
		Course c = new Course();
		CourseService courseService = new CourseService();
		c.setCname(cname);
		c.setCteacher(cteacher);
		c.setCstartdate(cstartdate);
		c.setCenddate(cenddate);
		c.setCpeople(cpeople);
		c.setCprice(cprice);
		c.setCplace(cplace);
		c.setCintroduce(cintroduce);
		
		courseService.insertCourse(c);
		
		RequestDispatcher rd = request
				.getRequestDispatcher("/course/DisplayCourse?type=MANAGER");
		rd.forward(request, response);
		return;
	}

}
